<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TagBase
 *
 * @author sadaf.siddiqui
 */
class TagBase extends CActiveRecordBehavior{
    
    // Set this to false to disable tracing of changes
    public $trace = true;
    // If you want to ignore some relations, set them here.
    public $ignoreRelations = array();
    // tag model name
    public $tagTable;
    
    public $relationalTable=null;
    
    public $ownerColumn =null;




    // A wrapper function for execution of SQL queries
	public function execute($query) {
		return Yii::app()->db->createCommand($query)->execute();
	}
        // A wrapper function for query of SQL statements
	public function query($query) {
		return Yii::app()->db->createCommand($query)->query();
	}
        
        // A wrapper function for query all rows  of SQL statement
	public function queryAll($query) {
		return Yii::app()->db->createCommand($query)->queryAll();
	}
        
        protected function addTags($value) {
		return sprintf("insert into `%s` (`name`) values ('%s')",
                $this->tagTable,$value);
	}
        
        protected function tagExist($tag){
            return $this->execute("select id from $this->tagTable where name='$tag'");
           
        }
        
        protected function fetchAll($values) {
		return $this->queryAll("Select id,name from $this->tagTable where name in ('".implode("','", $values)."')");
	}
        
        protected function addRelatedTags($values) {
		return sprintf("insert into `%s` (`tag_id`,`%s`) values %s",
                $this->relationalTable,  $this->ownerColumn,implode(",", $values));
	}
	
        protected function clearTags($ownerId) {
		return sprintf("delete from  `%s` where  `%s` = %s",
                $this->relationalTable,  $this->ownerColumn, $ownerId );
	}
        
        protected function fetchRelatedTagsCommaSeperated($ownerId) {
		$result=$this->queryAll("SELECT GROUP_CONCAT(tg.name) As tags FROM bjn_portfolio_tag pt left join bjn_tag tg on tg.id=pt.tag_id WHERE portfolio_id = $ownerId");
                return $result[0]['tags'];//$result->tags;
	}

}

?>
